<template>
	<view class="container">
		<view class="left-bottom-sign"></view>
		<!-- <view class="back-btn yticon icon-zuojiantou-up" @click="navBack"></view> -->
		<view class="right-top-sign"></view>
		<!-- 设置白色背景防止软键盘把下部绝对定位元素顶上来盖住输入框等 -->
		<view class="wrapper">
			<view class="left-top-sign">ROOM</view>
			<view class="welcome">
				绑定房间！
			</view>
			<view class="input-content">
				<view class="input-item">
					<text class="tit">小区名称</text>
					<picker @change="changeCommunity" :range="communities" range-key="areaName" class="input-class">
						<view :style="fontColor">{{communityName}}</view>
					</picker>
				</view>
				<view class="input-item">
					<text class="tit">楼栋信息</text>
					<picker @change="changeBuilding" :range="buildingInfo" range-key="name" class="input-class">
						<view :style="fontColor">{{buildingName}}</view>
					</picker>
				</view>
				<view class="input-item">
					<text class="tit">单元信息</text>
					<picker @change="changeUnit" :range="unitsInfo" range-key="unitNum" class="input-class">
						<view :style="fontColor">{{unitName}}</view>
					</picker>
				</view>
				<view class="input-item">
					<text class="tit">房屋信息</text>
					<picker @change="changeRoom" :range="roomsInfo" range-key="roomNum" class="input-class">
						<view :style="fontColor">{{roomName}}</view>
					</picker>
				</view>
				<view class="input-item">
					<text class="tit">成员关系</text>
					<picker @change="changeRelation" :range="relations" range-key="value" class="input-class">
						<view :style="fontColor">{{relationName}}</view>
					</picker>
				</view>
			</view>
			<button class="confirm-btn" @click="addApplication" :disabled="submitting">提交</button>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				communityName: "请选择小区",
				buildingName: "请选择楼栋编号",
				unitName: "请选择单元编号",
				roomName: "请选择房屋编号",
				relationName: "请选择成员关系",
				communityId: "",
				buildingId: "",
				unitId: "",
				layerId: "",
				roomId: "",
				relationId: "",
				communities: [],
				buildingInfo: [],
				unitsInfo: [],
				roomsInfo: [],
				relations: [{
					id: "1001",
					value: "业主"
				}, {
					id: "1002",
					value: "成员"
				}],
				fontColor: "color:#888888",
				applicationInfo: {},
				userInfo: uni.getStorageSync("userInfo"),
				applicationId: "",
			}
		},
		onLoad(options) {
			if (options && options.applicationId) {
				this.applicationId = options.applicationId;
			}
			this.getCommunityInfo()
		},
		methods: {
			//获取所有小区
			getCommunityInfo() {
				let params = {
					areaCode: this.$api.request.communityCode,
					areaLevel: 15
				}
				this.$api.request.getResidenceByAreaCode(params, (res) => {
					if (res.body.status.statusCode === '0') {
						this.communities = res.body.data.list;
					}
				})
			},
			getBuildingByCommunityId() {
				let params = {
					communityId: this.communityId
				}
				this.$api.request.getFloorsByCommunityId(params, (res) => {
					if (res.body.status.statusCode == this.$constant.STATUS_CODE_SUCCESS) {
						this.buildingInfo = res.body.data.floorEntities
					}
				})
			},
			getUnitByBuildingId() {
				let params = {
					floorId: this.buildingId
				}
				this.$api.request.getUnitsByFloorId(params, (res) => {
					if (res.body.status.statusCode == this.$constant.STATUS_CODE_SUCCESS) {
						this.unitsInfo = res.body.data.unitEntities
					}
				})
			},
			getRoomByUnitId() {
				let params = {
					unitId: this.unitId
				}
				this.$api.request.getRoomsByUnitId(params, (res) => {
					if (res.body.status.statusCode == this.$constant.STATUS_CODE_SUCCESS) {
						this.roomsInfo = res.body.data.roomEntities
					}
				})
			},
			changeCommunity(e) {
				this.clearPicker("COMMUNITY")
				let index = e.detail.value;
				this.communityName = this.communities[index].areaName;
				this.communityId = this.communities[index].areaCode;
				this.getBuildingByCommunityId()
			},
			changeBuilding(e) {
				this.clearPicker("BUILDING")
				this.buildingName = this.buildingInfo[e.detail.value].name
				this.buildingId = this.buildingInfo[e.detail.value].floorId
				this.getUnitByBuildingId()
			},
			changeUnit(e) {
				this.clearPicker("UNIT")
				this.unitName = this.unitsInfo[e.detail.value].unitNum + "单元";
				this.unitId = this.unitsInfo[e.detail.value].unitId
				this.getRoomByUnitId()
			},
			changeRoom(e) {
				let roomInfo = this.roomsInfo[e.detail.value];
				this.roomId = roomInfo.roomId;
				this.roomName = roomInfo.roomNum + "号";
			},
			changeRelation(e) {
				let relation = this.relations[e.detail.value];
				this.relationId = relation.id;
				this.relationName = relation.value
			},
			addApplication() {
				if (this.communityId == "") {
					this.$api.msg("请选择小区信息")
					return;
				}
				if (this.buildingId == "") {
					this.$api.msg("请选择楼栋信息")
					return;
				}
				if (this.unitId == "") {
					this.$api.msg("请选择单元信息")
					return;
				}
				if (this.roomId == "") {
					this.$api.msg("请选择房屋信息")
					return;
				}
				if (this.relationId == "") {
					this.$api.msg("请选择成员信息")
					return;
				}
				let params = {
					"id": this.applicationId,
					"areaCode": this.communityId,
					"userId": this.userInfo.userUuid,
					"roomId": this.roomId,
					"relationId": this.relationId,
					"roomInfo": this.buildingName + "-" + this.unitName + "-" + this.roomName,
					"remark": "小程序端绑定房屋"
				}
				// 修改审核记录
				this.$api.request.userApplicationAddRoomInfo(params, (res) => {
					let code = res.body.status.statusCode;
					if (code == this.$constant.STATUS_CODE_SUCCESS) {
						this.$api.msg("已提交至管理员，等待审核")
						setTimeout(() => {
							uni.redirectTo({
								url: "/package_isp/pages/family/houseList"
							})
						}, 1000)
					} else if (code == 1) {
						this.$api.msg("该房间的审核正在进行中，请勿重复绑定！")
					} else if (code == 2) {
						this.$api.msg("该房间您已经绑定过了，请勿重复绑定！")
					} else if (code == 3) {
						this.$api.msg("请先绑定小区")
					} else {
						this.$api.msg("系统出错了")
					}
				})
			},
			clearPicker(level) {
				switch (level) {
					case "COMMUNITY":
						this.buildingName = "请选择楼栋编号";
						this.unitName = "请选择单元编号";
						this.layerName = "请选择楼层编号";
						this.roomName = "请选择房屋编号";
						this.buildingId = "";
						this.unitId = "";
						this.layerId = "";
						this.roomId = "";
						this.buildingInfo = [];
						this.unitsInfo = [];
						this.layerInfo = [];
						this.roomsInfo = [];
						break;
					case "BUILDING":
						this.unitName = "请选择单元编号";
						this.layerName = "请选择楼层编号";
						this.roomName = "请选择房屋编号";
						this.unitId = "";
						this.layerId = "";
						this.roomId = "";
						this.unitsInfo = [];
						this.layerInfo = [];
						this.roomsInfo = [];
						break;
					case "UNIT":
						this.layerName = "请选择楼层编号";
						this.roomName = "请选择房屋编号";
						this.layerId = "";
						this.roomId = "";
						this.layerInfo = [];
						this.roomsInfo = [];
						break;
					default:
						break;
				}
			}
		}

	}
</script>

<style lang='scss'>
	page {
		background: #fff;
	}

	.container {
		padding-top: 45upx;
		position: relative;
		width: 100vw;
		height: 100vh;
		overflow: hidden;
		background: #fff;
		/* overflow: hidden; */
		/* overflow-y: scroll; */
	}

	.wrapper {
		position: relative;
		z-index: 90;
		/* background: #fff; */
		padding-bottom: 40upx;
	}

	.back-btn {
		position: absolute;
		left: 40upx;
		z-index: 9999;
		padding-top: var(--status-bar-height);
		top: 40upx;
		font-size: 40upx;
		color: $font-color-dark;
	}

	.left-top-sign {
		font-size: 120upx;
		color: $page-color-base;
		position: relative;
		left: -16upx;
	}

	.right-top-sign {
		position: absolute;
		top: 80upx;
		right: -30upx;
		z-index: 95;

		&:before,
		&:after {
			display: block;
			content: "";
			width: 400upx;
			height: 80upx;
			background: #b4f3e2;
		}

		&:before {
			transform: rotate(50deg);
			border-radius: 0 50px 0 0;
		}

		&:after {
			position: absolute;
			right: -198upx;
			top: 0;
			transform: rotate(-50deg);
			border-radius: 50px 0 0 0;
			/* background: pink; */
		}
	}

	.left-bottom-sign {
		position: absolute;
		left: -270upx;
		bottom: -320upx;
		border: 100upx solid #d0d1fd;
		border-radius: 50%;
		padding: 180upx;
	}

	.welcome {
		position: relative;
		left: 50upx;
		top: -50upx;
		font-size: 46upx;
		color: #555;
		text-shadow: 1px 0px 1px rgba(0, 0, 0, .3);
	}

	.input-content {
		padding: 0 60upx;
	}

	.input-code {
		display: inline-block;
		width: 100%;
		padding: 8upx 30upx 0 30upx;
		height: 120upx;
		background: $page-color-light;
		border-radius: 4px;
		margin-bottom: 30upx;

		.tit {
			height: 50upx;
			line-height: 56upx;
			font-size: $font-sm+2upx;
			color: $font-color-base;
		}
	}

	.input-item {
		display: flex;
		flex-direction: column;
		align-items: flex-start;
		justify-content: center;
		padding: 0 30upx;
		background: $page-color-light;
		height: 120upx;
		border-radius: 4px;
		margin-bottom: 30upx;

		&:last-child {
			margin-bottom: 0;
		}

		.tit {
			height: 50upx;
			line-height: 56upx;
			font-size: $font-sm+2upx;
			color: $font-color-base;
		}



		.mobileno-input {
			width: 100%;

			.mobileno-input-left {
				padding-top: 8upx;
				display: inline-block;
				width: 65%;
			}

			.mobileno-input-right {
				float: right;
				height: 60px;
				line-height: 60px;
				width: 35%;
				text-align: center;

				.sendCodeBtn {
					font-size: $font-sm+2upx;
					color: $font-color-base;
				}
			}
		}

		.input-class {
			height: 60upx;
			line-height: 50upx;
			font-size: $font-base + 2upx;
			color: $font-color-dark;
			width: 100%;
		}
	}

	.confirm-btn {
		width: 630upx;
		height: 76upx;
		line-height: 76upx;
		border-radius: 50px;
		margin-top: 60upx;
		background: $uni-color-primary;
		color: #fff;
		font-size: $font-lg;

		&:after {
			border-radius: 100px;
		}
	}

	.forget-section {
		font-size: $font-sm+2upx;
		color: $font-color-spec;
		text-align: center;
		margin-top: 40upx;
	}

	.register-section {
		position: absolute;
		left: 0;
		bottom: 50upx;
		width: 100%;
		font-size: $font-sm+2upx;
		color: $font-color-base;
		text-align: center;

		text {
			color: $font-color-spec;
			margin-left: 10upx;
		}
	}
</style>
